package com.xuanyuanxinyu.core.task;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
* @FileName:
* @Description:
* @author ken.luo
* @date 2023/4/4
* @version V1.0
* @email 424533553@qq.com
*/
public abstract class TaskExecutor {

    /**
     * 任务
     */
    public abstract void schedule(Runnable runnable);

    public abstract void scheduleSerial(Runnable r);

    public abstract void scheduleSerial(String group, Runnable r);

    public abstract void scheduleSerial(String group, Runnable r, boolean onlyRecent);

    /**
     * 延迟执行
     *
     */
    public abstract ScheduledFuture<Object> schedule(Runnable command, long delay, TimeUnit unit);


    /**
     * 定时执行 固定时间执行下一个
     *
     * @param command
     * @param initialDelay
     * @param period
     * @param unit
     * @return
     */
    public abstract ScheduledFuture<Object> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);

    /**
     * 定时执行 每一次执行完成之后在延迟执行下一个
     *
     * @param command
     * @param initialDelay
     * @param delay
     * @param unit
     * @return
     */
    public abstract ScheduledFuture<Object> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);

    public abstract void postToMainThread(Runnable runnable);

    public abstract void postToMainThreadDelay(Runnable runnable, long delayTime);

    public abstract void removeRunnable(Runnable runnable);

    public abstract boolean hasCallbacks(Runnable runnable);

    public void executeOnMainThread(Runnable runnable) {
        if (isMainThread()) {
            runnable.run();
        } else {
            postToMainThread(runnable);
        }
    }

    public abstract boolean isMainThread();
}
